package com.leetcode.LC;

import java.util.HashSet;

public class LC821 {
    public int[] shortestToChar(String s, char c) {
        //用List记录c出现的位置，用s字符位置分别减去这些值的绝对值的最小数放置数组中

        int[] ints = new int[s.length()];

        HashSet<Integer> set = new HashSet<>();

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c) {
                set.add(i);
            }
        }

        int min;
        for (int i = 0; i < s.length(); i++) {
            min = Integer.MAX_VALUE;
            if (s.charAt(i) == c) {
                ints[i] = 0;
            } else {
                for (Integer integer : set) {
                    min = Math.min(min, Math.abs(i - integer));
                }
                ints[i] = min;
            }
        }
        return ints;
    }

    public static void main(String[] args) {
        for (int i : new LC821().shortestToChar("loveleetcode", 'e')) {
            System.out.print(i + ",");
        }
    }
}
